namespace check_sum
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            string str_temp = richTextBox1.Text.Replace(" ", "");
            int str_len = str_temp.Length;
            //ushort u8_array[]=new ushort u8_array1[1024];
            if (str_temp.Length % 2 != 0)
            {
                str_temp += "0";
            }

            for (int k = 0; k < str_temp.Length; k++)
            {
                if (str_temp[k] < '0'|| str_temp[k]>'f'||(str_temp[k]>'9'&& str_temp[k]<'A')||(str_temp[k]>'F'&& str_temp[k]<'a'))
                {
                    textBox1.Text = "illegal data";
                    textBox2.Text = "illegal data";
                    textBox4.Text = "illegal data";
                    textBox5.Text = "illegal data";

                    return;
                }
            }
            //12=0x12
           
            
                UInt16 cs_reslut = 0;
                byte[] hex_data = new byte[str_temp.Length / 2];
                for (int i = 0; i < hex_data.Length; i++)
                {
                    hex_data[i] = Convert.ToByte(str_temp.Substring(i * 2, 2), 16);
                }
                for (int j = 0; j < hex_data.Length; j++)
                {
                    cs_reslut += hex_data[j];
                }
                textBox1.Text = (cs_reslut & 0xff).ToString("X2");
                textBox2.Text = cs_reslut.ToString("X4");
            

            textBox5.Text = "calculate done";
            textBox4.Text = (str_temp.Length / 2).ToString();
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}